﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ROrganizer.Client.Crawling
{
    internal class LimitedStream : MemoryStream
    {
        private int left;

        public LimitedStream(int capacity)
            : base(capacity)
        {
            this.left = capacity;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            if (left == 0)
                throw new Exception();

            if (count > left)
                count = left;

            base.Write(buffer, offset, count);
            left -= count;
        }

        [Serializable]
        public sealed class Exception : ApplicationException
        {
        }
    }
}
